package com.gfxy.educational.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.gfxy.common.portal.entity.SysOrganization;
import com.gfxy.common.portal.service.OrganizationService;
import com.gfxy.commons.message.Message;
import com.gfxy.commons.message.MessageUtil;
import com.gfxy.educational.entity.EduClass;
import com.gfxy.educational.entity.EduGrade;
import com.gfxy.educational.service.ClassService;
import com.gfxy.educational.service.GradeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author: lijixin 2019-06-05
 */
@Controller
@RequestMapping("/educational/class")
public class ClassController {

    @Autowired
    private GradeService gradeService;

    @Autowired
    private OrganizationService organizationService;

    @Autowired
    private ClassService classServer;


    @RequestMapping("index.action")
    public String getIndex(){
        return "educational/class";
    }

    @RequestMapping("getCollegeAndGradeTree")
    public @ResponseBody JSONArray getCollegeAndGrade(){
        List<SysOrganization> colleges =  organizationService.getOrganizationByType("1");
        List<EduGrade> grades = gradeService.getAllGrade();
        
        JSONArray result = new JSONArray();
        for(SysOrganization college : colleges){
            JSONObject collegeJson = new JSONObject();
            collegeJson.put("id",college.getOrganizationId());
            collegeJson.put("parentId",college.getParentId());
            collegeJson.put("name",college.getName());
            result.add(collegeJson);
            for(EduGrade grade :grades){
                JSONObject gradeJson = new JSONObject();
                gradeJson.put("id",college.getOrganizationId() + "@" + grade.getGradeId());
                gradeJson.put("parentId",college.getOrganizationId());
                gradeJson.put("name",grade.getGradeName());
                gradeJson.put("collegeName",collegeJson.get("name"));
                result.add(gradeJson);
            }
        }
        return result;
    }

    @RequestMapping("getClassInfo")
    public @ResponseBody JSONArray getClassInfo(HttpServletRequest request){
        String collegeAndGradeId = request.getParameter("collegeAndGradeId");
        JSONArray result = new JSONArray();
        if(collegeAndGradeId.contains("@")){
            //按学院和年级查询
            String[] collegeAndGradeIds = collegeAndGradeId.split("@");
            String collegeId = collegeAndGradeIds[0];
            String gradeId = collegeAndGradeIds[1];
            List<Map> queryResult = classServer.getClassViewInfoByCollegeAndGrade(collegeId,gradeId);
            for(Map map : queryResult){
                JSONObject object = getClassInfoJsonObject(map);
                result.add(object);
            }

        } else {
            //按年级查询
            List<Map> queryResult = classServer.getClassViewInfoByCollege(collegeAndGradeId);
            for(Map map : queryResult){
                JSONObject object = getClassInfoJsonObject(map);
                result.add(object);
            }

        }
        return result;
    }



    @RequestMapping("saveClassInfo")
    public @ResponseBody
    Message saveClassInfo(HttpServletRequest request){
        try{
            String classId = request.getParameter("classId");
            EduClass eduClass = new EduClass();
            eduClass.setClassName(request.getParameter("className"));
            eduClass.setGradeId(request.getParameter("gradeId"));
            eduClass.setOrganizationId(request.getParameter("collegeId"));
            eduClass.setState(Integer.parseInt(request.getParameter("state")));
            if(classId == null || classId.isEmpty()){
                int result = classServer.insertClassInfo(eduClass);
                return MessageUtil.getSuccessMsg(result);
            } else {
                eduClass.setClassId(classId);
                int result = classServer.updateClassInfo(eduClass);
                return MessageUtil.getSuccessMsg(result);
            }
        }catch (Exception e){
            return MessageUtil.getErrorMsg(e);
        }
    }

    @RequestMapping("deleteClassInfo")
    public @ResponseBody
    Message deleteClassInfo(HttpServletRequest request){
        try{
            String classId = request.getParameter("classId");
            int result = classServer.deleteClassInfoById(classId);
            return MessageUtil.getSuccessMsg(result);
        } catch (Exception e){
            return MessageUtil.getErrorMsg(e.getMessage());
        }

    }

    @RequestMapping("getCollege")
    public @ResponseBody JSONArray getCollege(){
        List<SysOrganization> organizations = organizationService.getOrganizationByType("1");
        JSONArray result = new JSONArray();
        for(SysOrganization organization : organizations){
            JSONObject object = new JSONObject();
            object.put("collegeId",organization.getOrganizationId());
            object.put("collegeName",organization.getName());
            result.add(object);
        }
        return result;
    }

    @RequestMapping("getGrade")
    public @ResponseBody JSONArray getGrade(){
        List<EduGrade> grades = gradeService.getAllGrade();
        JSONArray result = new JSONArray();
        for (EduGrade grade : grades) {
            JSONObject object = new JSONObject();
            object.put("gradeId",grade.getGradeId());
            object.put("gradeName",grade.getGradeName());
            result.add(object);
        }
        return result;
    }


    private JSONObject getClassInfoJsonObject(Map map) {
        JSONObject object = new JSONObject();
        object.put("classId",map.get("CLASS_ID"));
        object.put("className",map.get("CLASS_NAME"));
        object.put("collegeId",map.get("ORGANIZATION_ID"));
        object.put("collegeName",map.get("COLLEGE_NAME"));
        object.put("gradeId",map.get("GRADE_ID"));
        object.put("gradeName",map.get("GRADE_NAME"));
        object.put("state",map.get("STATE"));
        return object;
    }
}
